package abc.secgroup.music_backend.component;

import abc.secgroup.music_backend.entity.RoleDO;
import abc.secgroup.music_backend.entity.UserDO;
import abc.secgroup.music_backend.repository.UserRepository;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 这边具体实现了权限的接口
 * 在需要验证权限时请使用 StpUtil.hasPerssion(PERMISSION_CODE);
 */
@Component
public class StpInterfaceImpl implements StpInterface {
    final UserRepository userRepository;

    @Autowired
    public StpInterfaceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public List<String> getPermissionList(Object o, String s) {
        UserDO user = userRepository.findUserDOByAccount(StpUtil.getLoginIdAsString());
        List<String> list = new  ArrayList<>();
        if(user==null){//游客
            list.add("GUEST");
        }else{
            RoleDO role = user.getRole();
            String[] pess = role.getCode().split("\\|");
            Collections.addAll(list, pess);
        }
        return list;
    }

    @Override
    public List<String> getRoleList(Object o, String s) {
        return new ArrayList<>();
    }
}
